package net.wendal.nutzbook.module;

import cn.apiclub.captcha.Captcha;
import cn.apiclub.captcha.backgrounds.GradiatedBackgroundProducer;
import cn.apiclub.captcha.gimpy.FishEyeGimpyRenderer;
import net.wendal.nutzbook.util.Toolkit;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;

/**
 * Created by dengwubo on 2017/5/4.
 * raw代表RawView
 * png是RawView中对image/png的缩写,是数据mime的描述
 * 返回值是BufferedImage,且这是image/png, 所以会转为图片显示. 还支持jpg/webp等格式,详情参考RawView的源码吧.
 * Captcha有N多的组合和配置,自行选择啦.
 */
@IocBean
@At("/captcha")
public class CaptchaModule {
    private static  final Log log = Logs.get();
    @At
    @Ok("raw:png")
    public BufferedImage next (HttpSession session, @Param("w")int w,@Param("h") int h){
        if(w*h<1){// 长或宽为0？ 重置为默认长宽
            w=200;
            h=60;
        }
        Captcha captcha = new Captcha.Builder(w,h)
                .addText().addBackground(new GradiatedBackgroundProducer())
                .gimp(new FishEyeGimpyRenderer())
                .build();
        String text = captcha.getAnswer();
        log.debug("验证码："+text);
        session.setAttribute(Toolkit.captcha_attr,text);
        return  captcha.getImage();
    }

}
